package l1j.william;

import l1j.opqlo.Server.L1World.L1World;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.serverpackets.S_ServerMessage;
import l1j.server.server.serverpackets.S_SkillSound;
import java.util.Collection;
import l1j.server.server.model.L1Object;

public class CureArea extends Thread {
	private final int iMinX, iMinY, iMaxX, iMaxY, iMapID, iCureHp, iCureMp,
			iCurePeriod, iTimes, iGfxid;
	private l1j.server.server.model.Instance.L1PcInstance pc;

	public CureArea(final int minx, final int miny, final int maxx,
			final int maxy, final int mapid, final int mp, final int hp,
			final int cure_period, final int times, final int gfxid) {
		this.iMinX = minx; // TODO 最小x
		this.iMinY = miny; // TODO 最小y
		this.iMaxX = maxx; // TODO 最大x
		this.iMaxY = maxy; // TODO 最大y
		this.iMapID = mapid; // TODO 地圖號碼
		this.iCureHp = hp;// TODO 每回合治療點數 hp
		this.iCureMp = mp;// TODO 每回合治療點數 mp
		this.iTimes = times;// TODO 治療幾回合, 0代表無限
		this.iGfxid = gfxid;// TODO 魔法技能
		this.iCurePeriod = cure_period;// TODO 每回合要等幾秒
	}

	// @Override
	@Override
	public void run() {
		if (this.iTimes != 0) {
			for (int i = 0; i < this.iTimes; i++) {
				try {
					sleep(this.iCurePeriod * 1000);
					final Collection<L1PcInstance> al1object = L1World
							.getInstance().getAllPlayers();
					for (final L1Object object : al1object) {
						if (object instanceof L1PcInstance) {
							this.pc = (l1j.server.server.model.Instance.L1PcInstance) object;
							if (this.pc.getX() >= this.iMinX
									&& this.pc.getX() <= this.iMaxX
									&& this.pc.getY() >= this.iMinY
									&& this.pc.getY() <= this.iMaxY
									&& this.pc.getMapId() == this.iMapID) {
								if (!this.pc.isDead()) {
									if (this.pc.getCurrentHp() < this.pc
											.getBaseMaxHp()
											|| this.pc.getCurrentMp() < this.pc
													.getBaseMaxMp()) {
										this.pc.sendPackets(new S_SkillSound(
												this.pc.getId(), this.iGfxid));
										this.pc.broadcastPacketAll(new S_SkillSound(
												this.pc.getId(), this.iGfxid));
										this.pc.sendPackets(new S_ServerMessage(
												77, "")); // \f1気分が良くなりました。
										this.pc.setCurrentHp(this.pc
												.getCurrentHp() + this.iCureHp);
										this.pc.setCurrentMp(this.pc
												.getCurrentMp() + this.iCureMp);
									}
								}
							}
						}
					}
				} catch (final Exception ex) {
				}
			}
		} else {
			do {
				try {
					sleep(this.iCurePeriod * 1000);
					final Collection<L1PcInstance> al1object = L1World
							.getInstance().getAllPlayers();
					for (final L1Object object : al1object) {
						if (object instanceof L1PcInstance) {
							this.pc = (l1j.server.server.model.Instance.L1PcInstance) object;
							if (this.pc.getX() >= this.iMinX
									&& this.pc.getX() <= this.iMaxX
									&& this.pc.getY() >= this.iMinY
									&& this.pc.getY() <= this.iMaxY
									&& this.pc.getMapId() == this.iMapID) {
								if (!this.pc.isDead()) {
									if (this.pc.getCurrentHp() < this.pc
											.getBaseMaxHp()
											|| this.pc.getCurrentMp() < this.pc
													.getBaseMaxMp()) {
										this.pc.sendPackets(new S_SkillSound(
												this.pc.getId(), this.iGfxid));
										this.pc.broadcastPacketAll(new S_SkillSound(
												this.pc.getId(), this.iGfxid));
										this.pc.sendPackets(new S_ServerMessage(
												77, "")); // \f1気分が良くなりました。
										this.pc.setCurrentHp(this.pc
												.getCurrentHp() + this.iCureHp);
										this.pc.setCurrentMp(this.pc
												.getCurrentMp() + this.iCureMp);
									}
								}
							}
						}
					}
				} catch (final Exception ex) {
				}
			} while (true);
		}
	}
}